package com.util;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.util.service.RedisService;

@Component
public class RedisUtil implements RedisService {
	Logger log = Logger.getLogger(RedisUtil.class.getName());
	
	/*@Autowired
	Environment env;*/
	@Value("${config.redis.timeout}")
	private String timeout;
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Override
	public boolean stringSet(String key, String value) {
		boolean flag = false;
		try {
			stringSet(key, value, null);
			flag = true;
		}catch(Exception e){
			log.log(Level.SEVERE, "redis保存失败", e.getMessage());
		}
		return flag;
	}
	
	@Override
	public boolean stringSet(String key, String value, Long expireTime) {
		boolean flag = false;
		try {
			stringRedisTemplate.opsForValue().set(key, value);
			stringRedisTemplate.expire(key, Long.parseLong(timeout), TimeUnit.SECONDS);
			flag = true;
		}catch(Exception e){
			log.log(Level.SEVERE, "redis保存失败", e.getMessage());
		}
		return flag;
	}


	@Override
	public Object stringGet(String key) {
		return stringRedisTemplate.opsForValue().get(key);
	}
	
	
	@Override
	public void set(String key, Object value) {
		redisTemplate.opsForValue().set(key, value);
	}
	
	@Override
	public void set(String key, Object... value) {
		redisTemplate.opsForSet().add(key, value);
	}

	@Override
	public Object get(String key) {
		return redisTemplate.opsForValue().get(key);
	}

	
}
